package com.tul.vapp.servicecenter.client;

/**
 * 获取配置客户端
 * 
 * @author wwxiang
 * @since 2018/1/3.
 */
public class ClientFactory {

	private static IConfigClient client;

	private static String app;

	private static boolean startRemote = false;

	/**
	 * 服务启动时调用
	 * 
	 * @param appId 应用ID
	 */
	public static void init(String appId) {
		if (ClientFactory.app != null) {
		    // 防止多次初始化
			return;
		}
		ClientFactory.app = appId;
	}

    /**
     * 默认不会从ZK上获取配置和报告服务状态，除非调用此方法开启
     */
    public static void startRemote() {
        ClientFactory.startRemote = true;
    }

    /**
	 * 获取配置客户端(单例)
	 * 
	 * @return 配置客户端
	 */
	public static IConfigClient getClient() {
		if (client == null) {
			synchronized (ClientFactory.class) {
				if (client == null) {
					if (app == null) {
						throw new IllegalStateException("Pls config appId for ConfigClient.");
					}
					client = new ConfigClient(app, startRemote);
				}
			}
		}
		return client;
	}
}
